<?php
/**
 * Date element
 *
 */
class ArrowFormDate extends ArrowFormField{

	
	public function configure(){
		parent::configure();	
		$this->addProperties(array(
			"length" => "1,",
			"showTime" => "0",
			"displayDefaults" => "1",
			"selectedFn" => false
		));
	}	
	
	public function init(){
		parent::init();
		$jsData = array( "selectedFn" => $this->getProperty("selectedFn") );
		$this->registerJs( get_class($this), dirname(__FILE__)."/ArrowFromDate.js", "#".self::CTRL_NAMESPACE."-form-date".$this->getId(), $jsData);
	}
	
	public function validate( $proposedValue ){
		return true;
	}
	
	public function generateOutput(){
		
		$date = $time = $year = $month = $day = $hour = $minute = $second = "";
		$val = $this->getValue();
		
		if(empty($val) && $this->getProperty("displayDefaults")){
			if($this->getProperty("showTime"))
				$val = date("Y-m-d H:00:00");
			else
				$val = date("Y-m-d");
		}

		if( !empty($val) ){
			if($this->getProperty("showTime")){
				list( $date, $time ) = explode(" ", $val);
				list( $year, $month, $day ) = explode( "-", $date );
				try {
		          list( $hour, $minute, $second ) = explode( ":", $time );
		        } catch( Exception $e ) {
		          list( $hour, $minute ) = explode( ":", $time );
		          $second = 0 ;
		        }
			}else{
				$date = explode(" ", $val);
				$date = $date[0];
				list( $year, $month, $day ) = explode( "-", $date );
			}
		}
		
		$attribuites = array();
		$attribuites["class"] = self::CTRL_NAMESPACE."-form-date";
		$attribuites["id"] = self::CTRL_NAMESPACE."-form-date".$this->getId() ;
		$str = '<span '.$this->genTagsFromArray($attribuites).' >';

		$attribuites = array();
		$attribuites["class"] = self::CTRL_NAMESPACE."-form-date-date";
		$attribuites["value"] = $day."-".$month."-".$year;
		$str.= '<input type="text" '.$this->genTagsFromArray($attribuites).' '.$this->addExternal().' />';
		
		if($this->getProperty("showTime")){
			
			$optionsHour = "";
			for($i = 0;$i<=24;$i++){
				$hourD = str_pad($i, 2, "0", STR_PAD_LEFT);
				$optionsHour.= '<option calue="'.$hourD.'"  '.(($i==$hour)?'selected="selected"':'').' >'.$hourD.'</option>';
			}
				
			$optionsMinutes = "";
			for($i = 0;$i<60;$i+=15){
				$minuteD = str_pad($i, 2, "0", STR_PAD_LEFT);
				$optionsMinutes.= '<option calue="'.$minuteD.'" '.(($i==$minute)?'selected="selected"':'').' >'.$minuteD.'</option>';
			}
	
			$attribuites = array();
			$attribuites["class"] = self::CTRL_NAMESPACE."-form-date-hour";
			$str.='<select '.$this->genTagsFromArray($attribuites).' >'.$optionsHour.'</select>';
			
			$attribuites = array();
			$attribuites["class"] = self::CTRL_NAMESPACE."-form-date-minutes";	
			$str.='<select '.$this->genTagsFromArray($attribuites).' >'.$optionsMinutes.'</select>';
			
		}
		
		$attribuites = array(); 
		$attribuites["name"] = $this->getHtmlName();
		
		$attribuites["value"] = $val;
		$attribuites["type"] = "hidden";
		$attribuites["class"] = self::CTRL_NAMESPACE."-date-hidden";
		$str.= '<input '.$this->genTagsFromArray($attribuites).' />';
		
		$str.="</span>";
		
		
		
		return $str;
	}
	
}
?>